package com.sp.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import com.sp.action.base.BaseJsonPageAction;
import com.sp.po.Dict;
import com.sp.service.DictService;
/**
 * 字典的Action类
 * @author Mo
 *
 */
@SuppressWarnings("serial")
public class DictAction extends BaseJsonPageAction<Dict> {
	private DictService dictService;	//条件查询框返回的值
	private String dictFlagSel = "";
	private Boolean success = true;	//返回给FORM
	private JSON jsonToResult = null;
	private Dict dict;
	private String msg;
	
	/**
	 * 条件查询框的方法
	 * @return
	 */
	@SuppressWarnings("static-access")
	public String queryDictFlag(){
		List<Dict> dictFlagResult = new ArrayList<Dict>();
		String jsonResult = "";
		dictFlagResult = dictService.queryDictByCond(dictFlagSel, dictService.NUMBER_NINE, dictService.NUMBER_NINE);
		//将查询出来的数据Json 格式化,适合ExtJS的数据显示格式
		for(int i = 0;i<dictFlagResult.size(); i++){
			if(jsonResult == "" || jsonResult.equals("")){
				jsonResult = "{'dict_Flag':'" + dictFlagResult.get(i) + "'}";
			}else{
				jsonResult += ",{'dict_Flag':'" + dictFlagResult.get(i) + "'}";
			}
		}
		jsonResult = "[" + jsonResult + "]";
		//将结果序列化
		jsonToResult = JSONSerializer.toJSON(jsonResult);
		this.success = true;
		return SUCCESS;
	}
	/**
	 * 添加/修改dict
	 * @return
	 */
	public String addDict(){
		if(dict.getDictId() == null || dict.getDictId().equals("")){
			if(dictService.saveDict(dict)){
				this.success = true;
				this.msg = "添加字典信息成功!";
			}else{
				this.msg = "已有相同记录,请重新添加!";
				this.success = false;
			}
		}else{
			if(dictService.saveOrUpdateDict(dict)){
				this.success = true;
			}else{
				this.msg = "已有相同记录,更新失败!!";
				this.success = false;
			}
		}
		dict = null;
		return SUCCESS;
	}
	/**
	 * 字典查询
	 * @return
	 */
	public String queryDict(){
		this.reflashResultModel();
		this.success = true;
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String deleteDict(){
		dictService.deleteDict(dict.getDictId());
		return SUCCESS;
	}
	/**
	 * 实现分页查询
	 * 实现父类的方法
	 */
	@Override
	protected List<Dict> listResultModel(int start,int length) {
		// TODO Auto-generated method stub
		List<Dict> list = dictService.queryDictByCond(dictFlagSel, start, length);
		return list;
	}
	/**
	 * 实现查询总的记录数
	 * 实现父类的方法
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	@Override
	protected int queryTotal() {
		// TODO Auto-generated method stub
		List list = dictService.queryDictByCond(dictFlagSel, dictService.NUMBER_TEN, dictService.NUMBER_TEN);
		Long l = Long.valueOf(list.get(0).toString());
		return l.intValue();
	}
	/**
	 * 返回结果集
	 */
	@Override
	public List<Dict> getResultModel() {
		// TODO Auto-generated method stub
		return resultModel;
	}
	//返回总纪录数
	@Override
	public int getTotalPage() {
		// TODO Auto-generated method stub
		return totalPage;
	}
	public void setDictFlagSel(String dictFlagSel) {
		this.dictFlagSel = dictFlagSel;
	}
	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}
	public JSON getJsonToResult() {
		return jsonToResult;
	}
	public Boolean getSuccess() {
		return success;
	}
	public void setDict(Dict dict) {
		this.dict = dict;
	}
	public Dict getDict() {
		return dict;
	}
	public String getMsg() {
		return msg;
	}
}
